package com.lalifa.googledate.ui

import android.content.Context
import android.net.http.HttpResponseCache
import androidx.multidex.MultiDex
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.lalifa.base.BaseApplication
import com.lalifa.ext.ActivityManager
import com.lalifa.extension.pk
import com.lalifa.googledate.api.UserBean
import com.lalifa.googledate.api.api.InitNet
import com.lalifa.googledate.util.UserManager
import java.io.File

class MApplication : BaseApplication() {
    companion object {
        private lateinit var INSTANCE: MApplication
        fun get() = INSTANCE
    }

    //谷歌登录
    val gso = GoogleSignInOptions
        .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestId()
        .requestIdToken("108303679919377941526")
        .requestProfile()
        .requestEmail() //可以获取到邮箱信息【这一步还挺有必要，不光可以获取到邮箱信息，还会影响到下面获取性别信息】
        .build()
    var mGoogle: GoogleSignInClient? = null
    fun getGoogle(): GoogleSignInClient {
        return mGoogle ?: GoogleSignIn.getClient(this, gso)
    }

    override fun onCreate() {
        super.onCreate()
        INSTANCE = this
        ActivityManager.getInstance().init(this)
        initConfig()
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    /**
     * 初始化日志打印工具 列表空界面
     */
    private fun initConfig() {

        ActivityManager.getInstance().init(this)
        val cacheDir = File(applicationContext.cacheDir, "http")
        HttpResponseCache.install(cacheDir, 1024 * 1024 * 128)
        val u = UserManager.get()
        if (u == null || u.token.isEmpty()) {
            InitNet.initNetHttp(this, "")
        } else {
            InitNet.initNetHttp(this, u.token)
        }
        //BuildConfig.isDebug
        mGoogle = GoogleSignIn.getClient(this, gso)
    }
}